package middleware

import (
	"github.com/gin-gonic/gin"
	"go-pan/internal/utils"
)

func LoginMiddleware() gin.HandlerFunc {
	// 校验user，如果是匿名用户则返回401
	return func(c *gin.Context) {
		userInfo, ok := utils.GetUserInfo(c)
		if !ok {
			c.JSON(401, gin.H{"error": "unauthorized"})
			c.Abort()
			return
		}
		if ok && userInfo.IsGuest {
			c.JSON(401, gin.H{"error": "unauthorized"})
			c.Abort()
			return
		}
		c.Next()
	}
}
